import React from 'react';
import { Toast } from 'antd-mobile-rn';
import axios from 'axios';

//全局配置
axios.defaults.baseURL = 'http://101.132.191.91:6030';
axios.defaults.headers.get['Content-Type'] = 'application/json';
axios.defaults.headers.post['Content-Type'] = 'application/json';
axios.defaults.headers.put['Content-Type'] = 'application/json';
axios.defaults.headers.delete['Content-Type'] = 'application/json';
//请求拦截器
axios.interceptors.request.use(config =>{
    if(config.method === 'post') {
        config.data = JSON.stringify(config.data);
    }
    console.log('request config:',config);
    return config;
},error =>{
    console.log('request error:',error);
    Toast.info('request error!', 1, null, false);
});

//响应拦截器
axios.interceptors.response.use(res =>{
    console.log('response:',res );
    if(res.status == 200){
        let result = res.data;
        if(result.code != 1){
            Toast.info(result.msg, 1, null, false);
        }else{
            return result.data;
        }
    }else{
        console.log('net error',res.status,res.statusText);
        Toast.info(res.statusText, 1, null, false);
    }
    return null;
},error =>{
    console.log('response error:',error);
    Toast.info('response error!', 1, null, false);
})